package com.lbc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbc.bean.dto.NewsDTO;
import com.lbc.bean.dto.PictureDTO;
import com.lbc.bean.vo.NewsVO;
import com.lbc.bean.vo.PictureVO;
import com.lbc.common.resp.ResponseResult;
import com.lbc.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-03-17 19:50
 */
@Api(tags = "图集接口")
@RestController
@RequestMapping("picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @RequiresRoles("admin")
    @ApiOperation("添加图集 , admin")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated PictureDTO pictureDTO){
        return ResponseResult.toResponse(pictureService.add(pictureDTO));
    }

    @RequiresRoles("admin")
    @ApiOperation("修改图集 , admin")
    @PutMapping("modify")
    public ResponseResult modify(@RequestBody PictureDTO pictureDTO){
        if (ObjectUtils.isEmpty(pictureDTO.getPictureId())) {
            return ResponseResult.error("id不能为空");
        }
        return ResponseResult.toResponse(pictureService.modify(pictureDTO));
    }

    @RequiresRoles("admin")
    @ApiOperation("删除图集 , admin")
    @DeleteMapping("/remove/{pictureId}")
    public ResponseResult remove(@PathVariable("pictureId") Integer pictureId){
        return ResponseResult.toResponse(pictureService.remove(pictureId));
    }


    @ApiOperation("图集列表")
    @GetMapping("list")
    public ResponseResult<PageInfo<PictureVO>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        return new ResponseResult<>(new PageInfo<>(pictureService.list()));
    }
}
